var isLogin = false;

$(function () {
    checkLogin();
});

function checkLogin() {
    checkToken();
    if (!isLogin && !isNotLoginPage()) {
        console.log("===========?>");
        window.location.href = "/login";
    }
}

function isNotLoginPage() {
    return window.location.href.indexOf("/login") >= 0;
}

function checkToken() {
    var access_token = window.localStorage.getItem("access_token");
    $.ajax({
        url: "/oauth/check_token?token=" + access_token,
        type: "get",
        async: false,
        error: function (data) {
            isLogin = false;
        },
        success: function (data) {
            isLogin = true;
        }

    })
}

function storeToken(tokenObj) {
    if (tokenObj) {
        isLogin = true;
    }
    window.localStorage.setItem("access_token", tokenObj.access_token);
    window.localStorage.setItem("token_type", tokenObj.token_type);
}

function getToken() {
    return window.localStorage.getItem("token_type") + " " + window.localStorage.getItem("access_token");
}
function getAccesstoken() {
    return window.localStorage.getItem("access_token");
}

function make_base_auth(user, password) {
    var tok = user + ':' + password;
    var hash = btoa(tok);
    return "Basic " + hash;
}

